export default {
  install: function(Vue, options = {}) {
    let debug = options.debug || false
    Vue.prototype.$antPreview = function(file, ref, images = [], index = 0, previewIndex = -1) {
      try {
        let refObj = this.$refs[ref]
        if (Array.isArray(refObj)) {
          refObj = refObj[index]
        }
        let dom = refObj._isVue ? refObj.$el : refObj
        if (dom && dom.$viewer) {
          let index =
            previewIndex === -1
              ? images.findIndex((e) => {
                  console.log(e)
                  let originSrc = e.thumbUrl || e.url // 如果是刚上传的，ant使用thumbUrl，一般的话是用url
                  let fileSrc = file.thumbUrl || file.url
                  return originSrc === fileSrc
                })
              : previewIndex
          if (index === -1) index = 0
          dom.$viewer.view(index)
        } else {
          throw new Error('没有预览实例！')
        }
      } catch (e) {
        if (debug) {
          console.group('错误信息：')
          console.error(e)
          console.log('预览组件用法：')
          console.log('1, 绑定v-viewer')
          console.log('2, 指定ref')
          console.log('3, 指定参数： 1:$event, 2:ref, 3:图片数组, 4: 如果是在v-for中使用，需要传入循环的下标index')
          console.groupEnd()
        }
      }
    }
    Vue.prototype.$preview = function(file, ref, images = [], index = 0) {
      try {
        let refObj = this.$refs[ref]
        if (Array.isArray(refObj)) {
          refObj = refObj[index]
        }
        let dom = refObj._isVue ? refObj.$el : refObj
        if (dom && dom.$viewer) {
          let index = images.findIndex((e) => {
            let originSrc = e.src || e.url
            let fileSrc = file.src || file.url
            return originSrc === fileSrc
          })
          if (index === -1) index = 0
          dom.$viewer.view(index)
        } else {
          throw new Error('没有预览实例！')
        }
      } catch (e) {
        if (debug) {
          console.group('错误信息：')
          console.error(e)
          console.log('预览组件用法：')
          console.log('1, 绑定v-viewer')
          console.log('2, 指定ref')
          console.log('3, 指定参数： 1:$event, 2:ref, 3:图片数组, 4: 如果是在v-for中使用，需要传入循环的下标index')
          console.groupEnd()
        }
      }
    }
  }
}
